﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
namespace GPMS.BLL
{
    using GPMS.Model;
    using Webdiyer.WebControls.Mvc;
using System.Web;
    public static class MessageHelper
    {
        public static MessageVM Get(Guid id, ExtendedGPMSDb db)
        {
            return db.Messages
                .Select(m => new MessageVM()
                {
                    Message = m,
                    ReceiverName = m.Receiver.UserName,
                    SenderName = m.Sender.UserName
                })
                .FirstOrDefault(m => m.Message.MessageID == id);
        }
        public static MessageVM Get(Guid userID,Guid id, ExtendedGPMSDb db)
        {
            MessageVM model= db.Messages
                .Select(m => new MessageVM()
                {
                    Message = m,
                    ReceiverName = m.Receiver.UserName,
                    SenderName = m.Sender.UserName
                })
                .FirstOrDefault(m => m.Message.MessageID == id);
            if (userID==model.Message.ReceiverID)
            {
                model.Message.IsRead = true;
                db.SaveChanges();
            }
            return model;
        }

        public static Guid Add(MessageVM messageVM, ExtendedGPMSDb db,HttpRequestBase Request)
        {
            Message saveModel = new Message();
            saveModel.Title = messageVM.Message.Title;
            saveModel.Context = messageVM.Message.Context;
            saveModel.SenderID = AccountHelper.GetLoginUserID(Request);
            saveModel.ReceiverID = AccountHelper.Get(messageVM.ReceiverName, db).UserID;
            saveModel.FileID = messageVM.Message.FileID;
            return Add(saveModel,db);
        }
        public static Guid Add(Message message, ExtendedGPMSDb db)
        {
            if (message.MessageID==Guid.Empty)
            {
                message.MessageID = Guid.NewGuid();
            }
            if (message.FileID==Guid.Empty)
            {
                message.FileID = null;
            }
            if (message.SenderID==Guid.Empty || message.ReceiverID==Guid.Empty)
            {
                throw new ArgumentException();
            }
            message.IsDeletedReceiver = false;
            message.IsDeletedSender = false;
            message.IsRead = false;
            message.CreateDate = DateTime.Now;
            
            db.Messages.AddObject(message);
            db.SaveChanges();
            return message.MessageID;
        }

        public static PagedList<MessageVM> GetListReceive(Guid userID, int page, ExtendedGPMSDb db)
        {
            return db.Messages
                .Where(m => m.ReceiverID == userID && m.IsDeletedReceiver == false)
                .OrderByDescending(m => m.CreateDate)
                .Select(m => new MessageVM()
                {
                    Message = m,
                    ReceiverName = m.Receiver.UserName,
                    SenderName = m.Sender.UserName
                })
                .ToPagedList(page, SettingHelper.Get(db).PageSize);
        }

        public static PagedList<MessageVM> GetListSend(Guid userID, int page, ExtendedGPMSDb db)
        {
            return db.Messages
                .Where(m => m.SenderID == userID && m.IsDeletedSender == false)
                .OrderByDescending(m=>m.CreateDate)
                .Select(m => new MessageVM()
                {
                    Message = m,
                    ReceiverName = m.Receiver.UserName,
                    SenderName = m.Sender.UserName
                })
                .ToPagedList(page, SettingHelper.Get(db).PageSize);
        }

        public static PagedList<MessageVM> GetListNotRead(Guid userID, int page, ExtendedGPMSDb db)
        {
            return db.Messages
                .Where(m => m.ReceiverID == userID && m.IsDeletedReceiver == false && m.IsRead==false)
                .OrderByDescending(m => m.CreateDate)
                .Select(m => new MessageVM()
                {
                    Message = m,
                    ReceiverName = m.Receiver.UserName,
                    SenderName = m.Sender.UserName
                })
                .ToPagedList(page, SettingHelper.Get(db).PageSize);
        }


        public static void DeletedReceiver(Guid id, ExtendedGPMSDb db)
        {
            Message message = db.Messages.FirstOrDefault(m => m.MessageID == id);
            message.IsDeletedReceiver = true;
            db.SaveChanges();
        }

        public static void DeletedSender(Guid id, ExtendedGPMSDb db)
        {
            Message message = db.Messages.FirstOrDefault(m => m.MessageID == id);
            message.IsDeletedSender = true;
            db.SaveChanges();
        }

        public static int GetNotReadCount(Guid userID, ExtendedGPMSDb db)
        {
            return userID == Guid.Empty ? 0 : db.Messages
                .Count(m => m.IsDeletedReceiver == false && m.IsRead == false && m.ReceiverID == userID);
        }
    }
}
